package com.study.a10threadsafe3;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyThread extends Thread {
    static int ticket = 0;

    static Lock lock = new ReentrantLock();


    @Override
    public void run() {

        while (true) {
            //加锁
            lock.lock();
            try {
                if (ticket == 100) {
                    break;
                } else {

                    Thread.sleep(100);

                    ticket++;
                    System.out.println(Thread.currentThread().getName() + "在卖第" + ticket + "号票");
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                //解锁
                lock.unlock();
            }
        }
    }
}



